﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BLL;
using Model;
using Function;

namespace UI.Module.Coin
{
    public partial class CoinApply : System.Web.UI.Page
    {
        protected ST_InvestorMoreBLL morebll = new ST_InvestorMoreBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindType();
                BankAccount();
                ST_InvestorMore model = morebll.GetModel(Method.CurrentUserID);
                if (model == null)
                {
                    MessageBox_Page McBx = new MessageBox_Page();//实例化对话框
                    string RemarkString;
                    RemarkString = "未找到对象";

                    McBx.Title = "系统提示";
                    McBx.AddContent(RemarkString);
                    McBx.Type = MsgType.Error;
                    McBx.AddUrl("返回", "../../Module/Coin/CoinManage.aspx", UrlType.Href);
                    MessageEvent.MessageBox(McBx, "../../ErrorPage/ShowMsg.aspx");
                }
                this.lblCoin.Text = model.Coin.ToString();//可提现金额
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //提现密码
            string txtpwd = this.txtpwd.Text;
            //账户余额信息
            ST_InvestorMore investorModel = new ST_InvestorMore();
            investorModel = morebll.GetModel(Method.CurrentUserID, Method.GetMD5(txtpwd, Function.Method.MD5EncryptBit.bit32));
            if (investorModel == null)
            {
                MessageBox_Page McBx = new MessageBox_Page();//实例化对话框
                string RemarkString;
                RemarkString = "提现密码输入错误，提现操作失败";

                McBx.Title = "系统提示";
                McBx.AddContent(RemarkString);
                McBx.Type = MsgType.Error;
                McBx.AddUrl("返回", "../../Module/Coin/CoinManage.aspx", UrlType.Href);
                MessageEvent.MessageBox(McBx, "../../ErrorPage/ShowMsg.aspx");
                return;
            }
            if (investorModel.Coin<=0)
            {
                MessageBox_Page McBx = new MessageBox_Page();//实例化对话框
                string RemarkString;
                RemarkString = "可提现金额小于零，提现操作失败";

                McBx.Title = "系统提示";
                McBx.AddContent(RemarkString);
                McBx.Type = MsgType.Error;
                McBx.AddUrl("返回", "../../Module/Coin/CoinManage.aspx", UrlType.Href);
                MessageEvent.MessageBox(McBx, "../../ErrorPage/ShowMsg.aspx");
                return;
            }

            int bankID = Convert.ToInt32(hidBankID.Value);
            if (bankID == 0)
            {
                MessageBox_Page McBx = new MessageBox_Page();//实例化对话框
                string RemarkString;
                RemarkString = "未选中提现银行信息";

                McBx.Title = "系统提示";
                McBx.AddContent(RemarkString);
                McBx.Type = MsgType.Error;
                McBx.AddUrl("返回", "../../Module/Coin/CoinManage.aspx", UrlType.Href);
                MessageEvent.MessageBox(McBx, "../../ErrorPage/ShowMsg.aspx");
                return;
            }
            //提现金额
            decimal txtcoin = Convert.ToDecimal(this.txtApplyCoin.Text.Trim());
            //当前时间
            string time = Method.CurrentTime;
            //银行信息
            ST_InvestorBankAccount bankModel = new ST_InvestorBankAccountBLL().GetModel(bankID);

            //提现申请
            ST_GetCoinApplyBLL bll = new ST_GetCoinApplyBLL();
            ST_GetCoinApply model = new ST_GetCoinApply();
            model.Code = "";
            model.InvestorID = Method.CurrentUserID;
            model.BankID = bankModel.BankID;
            model.BankCode = bankModel.BankCode;
            model.Owner = bankModel.BankCardOwner;
            model.Coin = investorModel.Coin + investorModel.GetedCoin;//账户余额  可提现金额+当前提现金额
            model.ApplyCoin = txtcoin;
            model.ApplyTime = time;
            model.AuditTime = "";
            model.AuditManagerID = 0;
            model.AuditNote = "";
            model.FinanceID = 0;
            model.PayTime = "";
            model.State = 1;
            model.Note = "";
            int rowID = bll.Add(model);
            if (rowID > 0)
            {
                string str_code = bll.GetCode("TX", rowID, 4);
                if (bll.Update(str_code, rowID))
                {
                    //资金变动
                    investorModel.Coin = Convert.ToDecimal(this.lblCoin.Text.Trim()) - txtcoin;
                    investorModel.GetedCoin = investorModel.GetedCoin + txtcoin;
                    morebll.Update(investorModel);
                    //资金变动记录
                    JL_ChangeCoinLogs jlModel = new JL_ChangeCoinLogs();
                    jlModel.InvestorID = Method.CurrentUserID;
                    jlModel.LogTime = time;
                    jlModel.ItemID = 2;
                    jlModel.ChangeCoin = txtcoin;
                    jlModel.Coin = morebll.GetModel(Method.CurrentUserID).Coin + morebll.GetModel(Method.CurrentUserID).GetedCoin;
                    jlModel.Note = "投资者申请提现，账户信息变动";
                    new JL_ChangeCoinLogsBLL().Add(jlModel);

                    MessageBox_Page McBx = new MessageBox_Page();//实例化对话框
                    string RemarkString;
                    RemarkString = "金额提现申请成功！";

                    McBx.Title = "系统提示";
                    McBx.AddContent(RemarkString);
                    McBx.Type = MsgType.Success;
                    McBx.AddUrl("返回", "../../Module/Coin/CoinManage.aspx", UrlType.Href);
                    MessageEvent.MessageBox(McBx, "../../ErrorPage/ShowMsg.aspx");
                }
            }
            else
            {
                MessageBox_Page McBx = new MessageBox_Page();//实例化对话框
                string RemarkString;
                RemarkString = "金额提现申请失败！";

                McBx.Title = "系统提示";
                McBx.AddContent(RemarkString);
                McBx.Type = MsgType.Error;
                McBx.AddUrl("返回", "../../Module/Coin/CoinManage.aspx", UrlType.Href);
                MessageEvent.MessageBox(McBx, "../../ErrorPage/ShowMsg.aspx");
            }
        }

        string Message = "";
        protected void BindType()
        {
            //银行卡类型
            DropDownList1.DataSource = BLL.Data_BindDateBLL.Page_BindDate(out Message, "DY_BankInfo", "Code", 1, 0, "", PubConstant.DataBaseTarget.JHShop);
            DropDownList1.DataTextField = "Note";
            DropDownList1.DataValueField = "Code";
            DropDownList1.DataBind();

        }
        protected void BankAccount()
        {
            DataSet ds = new ST_InvestorBankAccountBLL().GetList("InvestorID=" + Method.CurrentUserID);
            if (ds.Tables[0].Rows.Count > 0)
            {
                Repeater1.DataSource = ds;
                Repeater1.DataBind();

                ST_InvestorBankAccount accountmod = new ST_InvestorBankAccountBLL().GetbyIsDefault(1);
                this.hidBankID.Value = accountmod == null ? "0" : accountmod.id.ToString();
            }
        }
        protected void btnDelete_Click(object sender, CommandEventArgs e)
        {
            int itemId = int.Parse(e.CommandName);//得到需要删除的记录的编号（Id）
            new ST_InvestorBankAccountBLL().Delete(itemId);
            BankAccount();
        }
    }
}